package com.funtl.screw.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.funtl.screw.admin.entity.SysPost;
import com.funtl.screw.admin.service.SysPostService;
import com.funtl.screw.common.core.util.QueryRequest;
import com.funtl.screw.common.core.util.ResponseResult;
import com.funtl.screw.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 岗位信息表
 *
 * @author fxz
 * @since 2022-03-26 12:50:43
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/post")
@Api(value = "post", tags = "岗位信息表管理")
public class SysPostController {

    private final SysPostService sysPostService;

    /**
     * 获取岗位列表
     *
     * @return 岗位列表
     */
    @GetMapping("/list")
    public ResponseResult<List<SysPost>> listPosts() {
        return ResponseResult.ok(sysPostService.list(Wrappers.emptyWrapper()));
    }

    /**
     * 分页查询
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('sys_post_view')")
    public ResponseResult<Page<SysPost>> getSysPostPage(QueryRequest queryRequest, SysPost sysPost) {
        Page<SysPost> page = new Page<>(queryRequest.getCurrent(), queryRequest.getSize());
        return ResponseResult.ok(sysPostService.page(page, Wrappers.query(sysPost)));
    }

    /**
     * 通过id查询岗位信息表
     *
     * @param postId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{postId}")
    @PreAuthorize("@pms.hasPermission('sys_post_view')")
    public ResponseResult<SysPost> getById(@PathVariable("postId") Long postId) {
        return ResponseResult.ok(sysPostService.getById(postId));
    }

    /**
     * 新增岗位信息表
     *
     * @param sysPost 岗位信息表
     * @return R
     */
    @ApiOperation(value = "新增岗位信息表", notes = "新增岗位信息表")
    @SysLog("新增岗位信息表")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('sys_post_add')")
    public ResponseResult<Boolean> save(@RequestBody SysPost sysPost) {
        return ResponseResult.ok(sysPostService.save(sysPost));
    }

    /**
     * 修改岗位信息表
     *
     * @param sysPost 岗位信息表
     * @return R
     */
    @ApiOperation(value = "修改岗位信息表", notes = "修改岗位信息表")
    @SysLog("修改岗位信息表")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('sys_post_edit')")
    public ResponseResult<Boolean> updateById(@RequestBody SysPost sysPost) {
        return ResponseResult.ok(sysPostService.updateById(sysPost));
    }

    /**
     * 通过id删除岗位信息表
     *
     * @param postId id
     * @return R
     */
    @ApiOperation(value = "通过id删除岗位信息表", notes = "通过id删除岗位信息表")
    @SysLog("通过id删除岗位信息表")
    @DeleteMapping("/{postId}")
    @PreAuthorize("@pms.hasPermission('sys_post_del')")
    public ResponseResult<Boolean> removeById(@PathVariable Long postId) {
        return ResponseResult.ok(sysPostService.removeById(postId));
    }
}
